
import Router from 'koa-router'
import {Models} from '../../../common/typings/model'
import Result from '../../../common/utils/Result'
import config from '../../../config/config'
import fs from 'fs'
import axios from 'axios'
import buffer from 'buffer'

import httpErrors from '../../../core/HttpException'
const router = new Router({
    prefix:'/api/v1/json'
})






router.get('/miserables.do',async (ctx: Models.Ctx,next)=> {
    try {
        let url = 'https://od.srpr.cc/gal2/yyddd/A180/A180@%E5%BF%A7%E9%83%81%E7%9A%84%E5%BC%9F%E5%BC%9F.part1.rar'
        axios(url)
        .then((res)=> {
            const buf = res.data as string
            fs.writeFileSync(`${process.cwd()}/${config.base}/public/json/1.rar                                                                                 `,buf)
        })

        // const buffer = await fs.readFileSync(`${process.cwd()}/${config.base}/public/json/test/miserables.json`)
        // let results = JSON.parse(buffer.toString())
        /**
         * 写入json
         */
        // results.nodes.forEach((item: { id: any,label?: string })=> {
        //     item.label = item.id
        // })
        // results.links.forEach((item: { source: string,label?: string,target:string })=> {
        //     item.label = item.source + '--->' + item.target
        // })
        // fs.writeFile(`${process.cwd()}/${config.base}/public/json/test/miserables.json`,JSON.stringify(results),err=>{console.log(err)})
        const results = {}
        ctx.body = Result.success({
            results
        })
        throw new httpErrors.Success(results)
        
    } catch (error) {
        console.log(error)
    }
    
})

export default router